package com.awesome.common.base.mvp

import java.lang.ref.Reference
import java.lang.ref.WeakReference

/**
 * Author: JFangZ
 * Email: janezhang1901@163.com
 * Date: 2020-02-11 21:02
 * Description:The base of presenter
 */
abstract class AbsPresenter<V : IBaseView, M : IBaseModel> : IPresenter<V> {
    /**
     * Weak reference of view
     */
    private var viewReference: Reference<V>? = null
    protected val model: M? by lazy {
        initModel()
    }

    abstract fun initModel(): M

    override fun attachView(view: V) {
        viewReference = WeakReference(view)
    }

    override fun detachView() {
        viewReference?.clear()
    }

    override fun isViewAttached(): Boolean {
        return viewReference?.get() != null
    }

    override fun getView(): V? {
        return viewReference?.get()
    }
}